//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.farm.doc.service.impl;

import com.farm.core.auth.domain.LoginUser;
import com.farm.core.sql.query.DBRule;
import com.farm.core.sql.query.DataQuery;
import com.farm.core.time.TimeTool;
import com.farm.doc.dao.DocdraftDaoInter;
import com.farm.doc.domain.Docdraft;
import com.farm.doc.service.DocdraftServiceInter;
import com.farm.doc.service.DocdraftServiceInter.DRAFT_TYPE;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class DocdraftServiceImpl implements DocdraftServiceInter {
    @Resource
    private DocdraftDaoInter docdraftDaoImpl;
    private static final Logger log = Logger.getLogger(DocdraftServiceImpl.class);

    public DocdraftServiceImpl() {
    }

    @Transactional
    public Docdraft insertDocdraftEntity(Docdraft entity, LoginUser user) {
        entity.setCuser(user.getId());
        entity.setCtime(TimeTool.getTimeDate14());
        entity.setCusername(user.getName());
        entity.setEuser(user.getId());
        entity.setEusername(user.getName());
        entity.setEtime(TimeTool.getTimeDate14());
        entity.setPstate("1");
        return this.docdraftDaoImpl.insertEntity(entity);
    }

    @Transactional
    public Docdraft editDocdraftEntity(Docdraft entity, LoginUser user) {
        Docdraft entity2 = this.docdraftDaoImpl.getEntity(entity.getId());
        entity2.setEusername(entity.getEusername());
        entity2.setPcontent(entity.getPcontent());
        entity2.setPstate(entity.getPstate());
        entity2.setContent(entity.getContent());
        entity2.setEuser(entity.getEuser());
        entity2.setCuser(entity.getCuser());
        entity2.setCusername(entity.getCusername());
        entity2.setEtime(TimeTool.getTimeDate14());
        entity2.setCtime(entity.getCtime());
        entity2.setType(entity.getType());
        entity2.setId(entity.getId());
        this.docdraftDaoImpl.editEntity(entity2);
        return entity2;
    }

    @Transactional
    public void deleteDocdraftEntity(String id, LoginUser user) {
        this.docdraftDaoImpl.deleteEntity(this.docdraftDaoImpl.getEntity(id));
    }

    @Transactional
    public Docdraft getDocdraftEntity(String id) {
        return id == null ? null : this.docdraftDaoImpl.getEntity(id);
    }

    @Transactional
    public DataQuery createDocdraftSimpleQuery(DataQuery query) {
        DataQuery dbQuery = DataQuery.init(query, "FARM_DOC_DRAFT", "ID,EUSERNAME,PCONTENT,PSTATE,CONTENT,EUSER,CUSER,CUSERNAME,ETIME,CTIME");
        return dbQuery;
    }

    @Transactional
    public Docdraft getDocdraftEntityByType(DRAFT_TYPE type, String userid) {
        List<DBRule> list = new ArrayList();
        list.add(new DBRule("type", type.getKey(), "="));
        list.add(new DBRule("cuser", userid, "="));
        List<Docdraft> docdrafts = this.docdraftDaoImpl.selectEntitys(list);
        return !docdrafts.isEmpty() ? (Docdraft)docdrafts.get(0) : null;
    }

    @Transactional
    public void deleteDocdraft(DRAFT_TYPE type, String userid) {
        List<DBRule> list = new ArrayList();
        list.add(new DBRule("type", type.getKey(), "="));
        list.add(new DBRule("cuser", userid, "="));
        this.docdraftDaoImpl.deleteEntitys(list);
    }
}
